﻿/**
 * validatebox验证扩展
 */
$.extend($.fn.validatebox.defaults.rules, {
	
	character: {
		validator: function(value) {
			return /^[A-Za-z]*$/.test(value);
		},
		message: '请输入字母'
	}
	
});

/**
 * tab关闭时回收内存
 */
$.fn.panel.defaults.onBeforeDestroy = function() {
	var frame = $('iframe', this);
	try {
		if (frame.length > 0) {
			frame[0].contentWindow.document.write('');
			frame[0].contentWindow.close();
			frame.remove();
			if ($.browser.msie) {
				CollectGarbage();
			}
		} else {
			$(this).find('.combo-f').each(function() {
				var panel = $(this).data().combo.panel;
				panel.panel('destroy');
			});
		}
	} catch (e) {
	}
};

/**
 * 页面所有easyui组件渲染成功后，隐藏等待信息
 */
$.parser.onComplete = function() {
	window.setTimeout(function() {
		window.top.$.messager.progress('close');
	}, 200);
};

/**
 * 支持easyui的datagrid和treegrid列头右键--显示/隐藏
 */
var createGridHeaderContextMenu = function(e, field) {
	e.preventDefault();
	var grid = $(this);
	var headerContextMenu = this.headerContextMenu;
	var okCls = 'tree-checkbox1';
	var emptyCls = 'tree-checkbox0';
	if (!headerContextMenu) {
		var tmenu = $('<div>', { 'style' : 'width:100px' }).appendTo('body');
		var fields = grid.datagrid('getColumnFields');
		for (var i = 0; i < fields.length; i++) {
			var fildOption = grid.datagrid('getColumnOption', fields[i]);
			if (!fildOption.hidden) {
				$('<div>', { 'iconCls' : okCls, 'field' : fields[i] }).html(fildOption.title).appendTo(tmenu);
			} else {
				$('<div>', { 'iconCls' : emptyCls, 'field' : fields[i] }).html(fildOption.title).appendTo(tmenu);
			}
		}
		headerContextMenu = this.headerContextMenu = tmenu.menu({
			onClick : function(item) {
				var field = $(item.target).attr('field');
				if (item.iconCls == okCls) {
					grid.datagrid('hideColumn', field);
					$(this).menu('setIcon', {
						target : item.target,
						iconCls : emptyCls
					});
				} else {
					grid.datagrid('showColumn', field);
					$(this).menu('setIcon', {
						target : item.target,
						iconCls : okCls
					});
				}
			}
		});
	}
	headerContextMenu.menu('show', {
		left : e.pageX,
		top : e.pageY
	});
};
$.fn.datagrid.defaults.onHeaderContextMenu = createGridHeaderContextMenu;
$.fn.treegrid.defaults.onHeaderContextMenu = createGridHeaderContextMenu;
